package file_tcp;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * Created with IntelliJ IDEA.
 * ClassName: Client
 * Package: file_tcp
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-08-14
 * Time: 15:42
 */

//发送文件给服务器 服务器将文件保存到本地
public class Client {
    public static void main(String[] args) throws IOException {

        InetAddress inetAddress = InetAddress.getByName("127.0.0.1");

        //1.创建Socket
        Socket socket = new Socket(inetAddress, 8888);

        //发送图片 创建File实例来操作和FileInputStream
        File file = new File("img.png");
        //将本地文件 读到客户端来
        FileInputStream fis = new FileInputStream(file);

        //通过socket 获取输出流 在从客户端发送出去
        OutputStream outputStream = socket.getOutputStream();

        //通过out 发送数据
        byte[] buffer = new byte[1024];
        int len;
        while ((len = fis.read(buffer)) != -1) {
            outputStream.write(buffer,0,len);
        }

        System.out.println("数据发送完毕");
        //关闭资源
        outputStream.close();
        fis.close();
        socket.close();
    }
}
